package com.simple.service.impl;

import com.simple.common.Result;
import com.simple.common.enumdto.FundEnum;
import com.simple.dto.BillStatisticsDTO;
import com.simple.dto.FundStatisticsDTO;
import com.simple.mapper.BillMapper;
import com.simple.mapper.FundMapper;
import com.simple.pojo.Bill;
import com.simple.pojo.Fund;
import com.simple.service.AmountService;
import com.simple.service.FundService;
import com.simple.service.HomeService;
import com.simple.utils.SimpleUtil;
import com.simple.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * @ClassName HomeServiceImpl
 * @Author SimpleTree
 **/
@Service
public class HomeServiceImpl implements HomeService {

    @Autowired
    private AmountService amountService;
    @Resource
    private FundMapper fundMapper;
    @Resource
    private BillMapper billMapper;

    @Override
    public Double getTotalMoney(Integer userId) {
        return amountService.getTotalMoney(userId);
    }

    @Override
    public FundStatisticsDTO getFundStatistics(String fundName,Integer userId) {

        List<Fund> funds = fundMapper.getAllFunds(fundName, userId);

        FundStatisticsDTO fundStatisticsDTO = new FundStatisticsDTO("基金折线图","元");
        fundStatisticsDTO.setXaxis(Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,12));

        FundStatisticsDTO.ValueBean valueBean = new FundStatisticsDTO.ValueBean();
        valueBean.setFundName(fundName);

        Double[] doubles = SimpleUtil.initDouble(12);
        funds.forEach(fund -> doubles[TimeUtil.getMonth(fund.getUpdateTime()) - 1] = fund.getProfit());
        valueBean.setYValues(SimpleUtil.double2List(doubles));
        fundStatisticsDTO.setYaxis(valueBean);

        return fundStatisticsDTO;
    }

    @Override
    public BillStatisticsDTO getLastMonthOfBills(Integer userId) {
        BillStatisticsDTO billStatisticsDTO = new BillStatisticsDTO("账单","子账单");

        Example example = new Example(Bill.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId", userId);
        criteria.andGreaterThanOrEqualTo("costTime", TimeUtil.getFirstDayOfMonth());
        criteria.andLessThanOrEqualTo("costTime", TimeUtil.getLastDayOfMonth());

        List<Bill> bills = billMapper.selectByExample(example);

        BigDecimal eatting = new BigDecimal("0");
        BigDecimal rent = new BigDecimal("0");
        BigDecimal fare = new BigDecimal("0");
        BigDecimal parent = new BigDecimal("0");
        BigDecimal other = new BigDecimal("0");

        for (Bill bill : bills) {
            Integer billType = bill.getBillType();
            Double money = bill.getMoney();

            if (billType <= 8) {
                eatting = eatting.add(new BigDecimal(money + ""));
            } else if (billType == 9) {
                fare = fare.add(new BigDecimal(money + ""));
            } else if (billType == 10) {
                rent = rent.add(new BigDecimal(money + ""));
            } else if (billType <= 13) {
                parent = parent.add(new BigDecimal(money + ""));
            } else {
                other = other.add(new BigDecimal(money + ""));
            }
        }

        List<BillStatisticsDTO.BillValueDTO> billValueDTOs = new ArrayList<>();
        billValueDTOs.add(new BillStatisticsDTO.BillValueDTO("吃", eatting.doubleValue()));
        billValueDTOs.add(new BillStatisticsDTO.BillValueDTO("行", fare.doubleValue()));
        billValueDTOs.add(new BillStatisticsDTO.BillValueDTO("房租", rent.doubleValue()));
        billValueDTOs.add(new BillStatisticsDTO.BillValueDTO("亲人", parent.doubleValue()));
        billValueDTOs.add(new BillStatisticsDTO.BillValueDTO("其他", other.doubleValue()));

        billStatisticsDTO.setBillValueDTOs(billValueDTOs);
        billStatisticsDTO.setLegend(Arrays.asList("吃","行","房租","亲人","其他"));

        return billStatisticsDTO;
    }


}
